package com.android.questions.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.config.LoginConfig;
import com.android.questions.databinding.DialogAnswerAssistantBinding;
import com.android.questions.util.AnimUtil;

/**
 * 答题小助手
 * <p>
 * Author: admin
 * Date: 2021/11/16 16:05
 * Description:
 */
public class DialogAnswerAssistant extends Dialog implements View.OnClickListener {

    private DialogAnswerAssistantBinding answerAssistantBinding;
    private OnClickLister clickLister;
    private final Activity activity;

    public DialogAnswerAssistant(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        answerAssistantBinding = DialogAnswerAssistantBinding.inflate(activity.getLayoutInflater());
        setContentView(answerAssistantBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        ActivityClickUtil.getInstance().clear();
        AnimUtil.receiveAnim(activity, answerAssistantBinding.ivReceive);

        String tips;
        double userCash = LoginConfig.getUserCash();
        if (userCash >= 3000) {
            tips = "领取奖励后可再次提现哦！";
        } else {
            int xx = (int) ((3000 - userCash) / 300 + 1);
            tips = "预计再答<font color='#FD001A'>" + xx + "</font>题可再次提现！";
        }

        answerAssistantBinding.tvTips.setText(Html.fromHtml(tips));
    }

    private void initLister() {
        answerAssistantBinding.ivClose.setOnClickListener(this);
        answerAssistantBinding.ivReceive.setOnClickListener(this);
    }

    public DialogAnswerAssistant setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.iv_receive) {
            if (this.clickLister != null) {
                this.clickLister.onNext();
            }
        }
    }

    public interface OnClickLister {
        void onNext();
    }
}